
/**
 *
 * @param {*} exdays  
 *
 */

function setCookie(cName = "", cValue = "", exdays = "") {
  var exdate = new Date();
  exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
  if (!exdays) {
    window.document.cookie = cName + "=" + escape(JSON.stringify(cValue));
  } else {
    window.document.cookie =
      cName +
      "=" +
      escape(JSON.stringify(cValue)) +
      ";path=/;expires=" +
      exdate.toGMTString();
  }
}

function getCookie(name) {
  if (document.cookie.length > 0) {
    var allcookies = document.cookie;
    name += "=";
    var pos = allcookies.indexOf(name);
    if (pos != -1) {
      var start = pos + name.length;
      var end = allcookies.indexOf(";", start);
      if (end == -1) {
        end = allcookies.length;
      }
      var value = allcookies.substring(start, end);
      return JSON.parse(unescape(value));
    } else {
      return;
    }
  }
}

function clearCookie(name) {
  setCookie(name, "", -1);
}


function clearCookies() {
  var temp = document.cookie.split(";");
  var name;
  for (var i = 0; ; i++) {
    if (!temp[i]) break;
    name = temp[i].split("=")[0];
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    document.cookie = name + "= ''" + "; expires=" + exp.toGMTString();
  }
}
export { setCookie, getCookie, clearCookie, clearCookies };
